package cn.message.shiro;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;

/**
 * @author 郭智超
 * @create 2020-04-26 9:17
 * shiro的配置类
 */
@Configuration
@Slf4j
public class ShiroConfig {

    /**
     * 创建ShiroFilterFactoryBean
     * @param securityManager
     * @return
     */
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //设置安全管理器
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        //添加Shiro内置过滤器
        /**
         * Shiro内置过滤器，可以实现权限相关的 拦截器
         *    常用的过滤器：
         *       anon: 无需认证（登录）可以访问
         *       authc: 必须认证才可以访问
         *       user: 如果使用rememberMe的功能可以直接访问
         *       perms： 该资源必须得到资源权限才可以访问
         *       role: 该资源必须得到角色权限才可以访问
         */
        LinkedHashMap<String, String> filterMap = new LinkedHashMap<>();

        log.debug("开始执行授权过滤器");
        filterMap.put("/wxshop/message/login","anon");
        filterMap.put("/wxshop/message/noAuth","anon");
        //设置权限拦截器
        //配置商品模块的拦截器
        filterMap.put("/dhn/message/getAllClassify","perms[classify:get]");
        filterMap.put("/dhn/message/addMainClassify","perms[MainClassify:add]");
        filterMap.put("/dhn/message/addchildrenClassify","perms[ChildrenClassify:add]");
        filterMap.put("/dhn/message/updateClassify","perms[classify:update]");
        filterMap.put("/dhn/message/deleteClassifyById","perms[classify:delete]");
        filterMap.put("/dhn/message/updateIcon","perms[updateClassify:updateIcon]");

        //配置骑手模块的拦截器
        filterMap.put("/dhn/message/getAllHorseman","perms[horseman:get]");
        filterMap.put("/dhn/message/addHorseman","perms[horseman:add]");
        filterMap.put("/dhn/message/deleteHorsemanByHorsemanId","perms[horseman:delete]");
        filterMap.put("/dhn/message/updateHorseman","perms[horseman:update]");
        filterMap.put("/dhn/message/openAndCloseHorseman","perms[horseman:openOrClose]");

        //配置账户模块的拦截器
        filterMap.put("/dhn/message/getAllAccount","perms[account:get]");

        //配置收益模块的拦截器
        filterMap.put("/dhn/message/getAllEarnings","perms[earnings:get]");

        //配置充值模块的拦截器
        filterMap.put("/dhn/message/getAllRecharge","perms[recharge:get]");

        //配置提现模块的拦截器
        filterMap.put("/dhn/message/getAllWithdraw","perms[withdraw:get]");
        filterMap.put("/dhn/message/getOrder","perms[historyOrder:get]");
        filterMap.put("/dhn/message/sendCheck","perms[withdraw:check]");
        filterMap.put("/dhn/message/lostEffect","perms[withdraw:lostEffect]");

        //配置订单模块的拦截器
        filterMap.put("/wxshop/message/findAllOrder","perms[order:get]");
        filterMap.put("/wxshop/message/sendOrder","perms[order:distribute]");
        filterMap.put("/wxshop/message/findOrderItem","perms[orderItem:get]");
        filterMap.put("/wxshop/message/exportOrderExcel","perms[order:export]");

        //配置用户管理模块的拦截器
        filterMap.put("/dhn/message/getAllUser","perms[user:get]");

        //配置骑手绩效模块的拦截器
        filterMap.put("/dhn/message/getAllPerformance","perms[performance:get]");
        filterMap.put("/dhn/message/getAllPerformanceItem","perms[performanceItem:get]");

        //配置权限管理模块的拦截器
        filterMap.put("/dhn/message/getAllAdmin","perms[admin:get]");
        filterMap.put("/dhn/message/deleteAdmin","perms[admin:delete]");
        filterMap.put("/dhn/message/addAdmin","perms[admin:add]");
        filterMap.put("/dhn/message/updateAdmin","perms[admin:update]");
        filterMap.put("/dhn/message/uploadAdminHeader","perms[admin:uploadAdminHeader]");
        filterMap.put("/dhn/message/getAllRole","perms[role:get]");
        filterMap.put("/dhn/message/deleteRole","perms[role:delete]");
        filterMap.put("/dhn/message/addRole","perms[role:add]");
        filterMap.put("/dhn/message/updateRole","perms[role:update]");
        filterMap.put("/dhn/message/getPermissions","perms[permissions:get]");
        filterMap.put("/dhn/message/getRoles","perms[roles:get]");
        filterMap.put("/dhn/message/deleteRole","perms[role:delete]");

        //商家管理模块
        filterMap.put("/dhn/message/getAllMerchant","perms[merchant:get]");
        filterMap.put("/dhn/message/deleteMerchant","perms[merchant:delete]");
        filterMap.put("/dhn/message/addMainMerchant","perms[MainMerchant:add]");
        filterMap.put("/dhn/message/addChildrenMerchant","perms[ChildrenMerchant:add]");
        filterMap.put("/dhn/message/updateMerchant","perms[updateMerchant]");
        filterMap.put("/dhn/message/openAndCloseMerchant","perms[merchant:openAndCloseMerchant]");

        //打包订单模块
        filterMap.put("/dhn/message/getAllPackageOrder","perms[packageOrder:get]");
        filterMap.put("/dhn/message/getPackageOrderItem","perms[packageOrderItem:get]");
        filterMap.put("/dhn/message/exportPackageOrder","perms[packageOrder:export]");

        //区域管理模块
        filterMap.put("/dhn/message/getAllDistrict","perms[district:get]");
        filterMap.put("/dhn/message/openDistrict","perms[district:open]");
        filterMap.put("/dhn/message/closeDistrict","perms[district:close]");

        //帮助中心模块
        filterMap.put("/dhn/message/getAllHelp","perms[help:get]");
        filterMap.put("/dhn/message/addHelp","perms[help:add]");
        filterMap.put("/dhn/message/updateHelp","perms[help:update]");
        filterMap.put("/dhn/message/deleteHelp","perms[help:delete]");
        filterMap.put("/dhn/message/uploadImage","perms[help:updateIcon]");

        //轮播图管理模块
        filterMap.put("/dhn/message/getAllImage","perms[image:get]");
        filterMap.put("/dhn/message/addImage","perms[image:add]");
        filterMap.put("/dhn/message/updateImage","perms[image:update]");
        filterMap.put("/dhn/message/uploadPicture","perms[image:uploadPicture]");
        filterMap.put("/dhn/message/deleteImage","perms[image:delete]");

         //filterMap.put("/*","authc");
        //修改登录认证失败后跳转的登录页面
        //shiroFilterFactoryBean.setLoginUrl("/wxshop/message/tologin");



        //设置登录成功跳转的页面
        //shiroFilterFactoryBean.setSuccessUrl("/welcome");

        //设置未授权提示页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/wxshop/message/noAuth");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        log.debug("权限过滤器执行结束");
        return shiroFilterFactoryBean;
    }

    /**
     * 创建DefaultWebSecurityManager
     * @param adminRealm
     * @return
     */
    @Bean(name = "securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("adminRealm")AdminRealm adminRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联realm
        securityManager.setRealm(adminRealm);
        return securityManager;
    }

    /**
     * 创建realm
     * @return
     */
    @Bean(name = "adminRealm")
    public AdminRealm getRealm(){
        return new AdminRealm();
    }
}
